package de.philipprawe.trainingsplan.util;

import java.io.File;
import java.io.FileOutputStream;

import de.philipprawe.trainingsplan.beans.CaptureActivityBean;
import de.philipprawe.trainingsplan.beans.SessionBean;

public class FileIo {
	
	public static boolean writeFile(SessionBean session, File file){
		String xml = "";
		xml += "<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>";
		xml += "<TRAININGSPLAN>";
		xml += "<USERDATA>";
		xml += "<USERNAME>" + session.getUserData().getUserName() + "</USERNAME>";
		xml += "<HEIGTH>" + session.getUserData().getHeigth() + "</HEIGTH>";
		xml += "<BIRTHDAY>" + session.getUserData().getBirthday() + "</BIRTHDAY>";
		xml += "<ACCUSER>" + session.getUserData().getGoogleUserName() + "</ACCUSER>";
		xml += "<ACCPW>" + session.getUserData().getGooglePw() + "</ACCPW>";
		xml += "</USERDATA>";
		xml += "<ACTIVITIES>";
		for(CaptureActivityBean activityBean : session.getActivities()){
			xml += "<ACTIVITY>";
			xml += "<KIND>" + activityBean.getActivity() + "</KIND>";
			xml += "<DATE>" + activityBean.getDate() + "</DATE>";
			xml += "<PULSE>" + activityBean.getAvg_pulse() + "</PULSE>";
			xml += "</ACTIVITY>";
		}
		xml += "</ACTIVITIES>";
		xml += "</TRAININGSPLAN>";
//		String path = "C:\\Users\\ra\\Desktop\\tmp\\";
//		File parent = new File(path);
//		File parent = file;
//		if (!parent.exists()) {
//			parent.mkdirs();
//		}
		File f = file;
		FileOutputStream fos = null;
		try{
			fos = new FileOutputStream(f.getPath());
			fos.write(xml.getBytes());
			fos.flush();
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try{
				if (fos != null)
					fos.close();
			}catch(Exception e){}
		}
		return true;
	}
	
	public static boolean readFile(String filename){
		
		return true;
	}

}
